package com.example.web.demos;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.*;

/**
 * @author scout.du
 * @date 2024/07/03 14:40
 **/
public class RestTemplateTestCase {


    //http请求四要素：header body url 方法(get post)
    //http响应三要素：header body status_code(200 404 403 500)




    //设置header
    @Test
    public void test6(){

        RestTemplate restTemplate = new RestTemplate();

        String url = "https://app.dev.aws.gravity.porsche.cn/management-portal-dev-api/user/getUserInfo";

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.put("token", List.of("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiZTZiMmI3OWZhYzQ0YjE1YTZhNjkzNTJjZmVmZjE1MCIsInN1YiI6IjMiLCJpc3MiOiJtYW5hZ2VtZW50LXBvcnRhbC5ncmF2aXR5LnBvcnNjaGUuY24iLCJhdWQiOiJtYW5hZ2VtZW50LXBvcnRhbC5ncmF2aXR5LnBvcnNjaGUuY24iLCJpYXQiOjE3MjM3OTExNTcsImV4cCI6MTcyMzk2Mzk1NywidXNlcm5hbWUiOiJkYnVzZXJAZGJhdXRoLmNvbSIsInJvbGVzIjpbIkFkbWluIl0sInBlcm1pc3Npb25zIjpbIkRBU0hCT0FSRF9WSUVXIiwiQVBQX09WRVJWSUVXIiwiQVBQX0RFVEFJTF9WSUVXIiwiQVBQX0RFVEFJTF9MSVNUX1ZJRVciLCJQT0RfTE9HX1ZJRVciLCJSRVNPVVJDRV9ERVRBSUxfVklFVyIsIlBPRF9FRElUIiwiU0VSVklDRV9FRElUIiwiREVQTE9ZTUVOVF9FRElUIiwiQ09ORklHTUFQX0VESVQiLCJTRUNSRVRfRURJVCIsIkFQUF9NT05JVFJPSU5HIiwiQVBQX01PTklUT1JJTkdfVklFVyIsIkFQUF9MT0dfQ0VOVEVSX1ZJRVciLCJBVURJVCIsIk9QRVJBVEVfSElTVE9SWSIsIlBSRUZFUkVOQ0UiLCJNRVNTQUdFX0NFTlRFUiIsIlFVT1RBX0VESVQiLCJBRE1JTl9DT05TT0xFIiwiQVBQX0FETUlOIiwiVEFHX0FETUlOIiwiUk9MRV9BRE1JTiIsIlVTRVJfQURNSU4iLCJGRUVEQkFDS19ISVNUT1JZIiwiRkVFREJBQ0tfQ0VOVEVSIiwiQ0RfVklFVyIsIkNEX0VESVQiLCJJTkdSRVNTX0VESVQiLCJDRF9TRVRUSU5HUyIsIkNPU1RfRVhQX1ZJRVciLCJDT1NUX01PREVMX1ZJRVciLCJMT0dfQ09ORklHIiwiQ09TVF9GSU5BTkNFIl19.WnwFwgeeEeAxHAWhJyAtMmghrkWBGYGYgDYRDKAFMWk"));

        String requestBody = "{\"pageNum\":1,\"pageSize\":6,\"searchKey\":\"\",\"tagIds\":[]}";
        JSONObject body = JSONObject.parseObject(requestBody);


        RequestEntity requestEntity = new RequestEntity<>(body,headers,HttpMethod.POST,URI.create(url));
        ResponseEntity<String> entity = restTemplate.exchange(requestEntity, String.class);
        String response = entity.getBody();
        System.out.println(response);
    }

    //MultiValueMap-->"content-type":"application/x-www-form-urlencoded;charset=UTF-8",
    @Test
    public void test5(){

        /*
        * map.put("name", List.of("tom"));
        * map.put("age", List.of("18"));
        * name=tom&age=18
        * */

        /*
         * map.put("name", List.of("tom","jack"));
         * map.put("age", List.of("18"));
         * name=tom&name=jack&age=18
         * */

        /*
         * map.put("name", List.of("tom"));
         * map.put("name", List.of("jack"));
         * map.put("age", List.of("18"));
         * name=jack&age=18
         * */
        RestTemplate restTemplate = new RestTemplate();

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
//        map.put("name", List.of("tom"));
//        map.put("name", List.of("jack"));
        map.put("name", List.of("tom","jack"));
        map.put("age", List.of("18"));

        String response = restTemplate.postForObject("http://localhost:8081/system/addApp", map, String.class);
        System.out.println(response);
    }

    //Map-->"content-type":"application/json"
    @Test
    public void test4(){

        RestTemplate restTemplate = new RestTemplate();

        String requestBody = "{\"appName\":\"etmt\",\"envList\":[{\"envId\":7,\"namespace\":\"etmt\"},{\"envId\":8,\"namespace\":\"etmt\"},{\"envId\":9,\"namespace\":\"etmt\"},{\"envId\":10,\"namespace\":\"etmt\"}],\"userIdList\":[3,6,16,17,18,19,29]}";
        Map map = JSON.parseObject(requestBody, Map.class);

        String response = restTemplate.postForObject("http://localhost:8081/system/addApp", map, String.class);
        System.out.println(response);
    }

    //String-->"content-type":"text/plain;charset=ISO-8859-1",
    @Test
    public void test3(){

        RestTemplate restTemplate = new RestTemplate();

        String requestBody = "{\"appName\":\"etmt\",\"envList\":[{\"envId\":7,\"namespace\":\"etmt\"},{\"envId\":8,\"namespace\":\"etmt\"},{\"envId\":9,\"namespace\":\"etmt\"},{\"envId\":10,\"namespace\":\"etmt\"}],\"userIdList\":[3,6,16,17,18,19,29]}";
//        JSONObject jsonObject = JSON.parseObject(requestBody);

        String response = restTemplate.postForObject("http://localhost:8081/system/addApp", requestBody, String.class);
        System.out.println(response);
    }

    //Object-->"content-type":"application/json"
    @Test
    public void test2(){

        RestTemplate restTemplate = new RestTemplate();

        String requestBody = "{\"appName\":\"etmt\",\"envList\":[{\"envId\":7,\"namespace\":\"etmt\"},{\"envId\":8,\"namespace\":\"etmt\"},{\"envId\":9,\"namespace\":\"etmt\"},{\"envId\":10,\"namespace\":\"etmt\"}],\"userIdList\":[3,6,16,17,18,19,29]}";
        JSONObject jsonObject = JSON.parseObject(requestBody);

        String response = restTemplate.postForObject("http://localhost:8081/system/addApp", jsonObject, String.class);
        System.out.println(response);
    }

    //加拦截器
    @Test
    public void test1(){

        RestTemplate restTemplate = new RestTemplate();
        ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
            request.getHeaders().set("token", "eyJhbGciOi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omP9qPuHhe6dzOJdMk1xAMkBpYc");
            return execution.execute(request, body);
        };
        restTemplate.setInterceptors(List.of(interceptor));

        String requestBody = "{\"appName\":\"etmt\",\"envList\":[{\"envId\":7,\"namespace\":\"etmt\"},{\"envId\":8,\"namespace\":\"etmt\"},{\"envId\":9,\"namespace\":\"etmt\"},{\"envId\":10,\"namespace\":\"etmt\"}],\"userIdList\":[3,6,16,17,18,19,29]}";
//        List<String> list = Arrays.asList("digital-sales", "myporsche", "iccpd-hgih", "wallbox", "super-app", "gio (hc)", "datalake (hc)", "fs");
        List<String> list = Arrays.asList("pos");
        list.forEach(s -> {
            String body = requestBody.replace("etmt", s);
            System.out.println(body);
            JSONObject jsonObject = JSON.parseObject(body);
            String response = restTemplate.postForObject("http://localhost:8081/system/addApp", jsonObject, String.class);
            System.out.println(response);
        });

    }

}
